package com.suncnpap.system.controller;

import com.suncnpap.system.model.Config;
import com.suncnpap.system.model.School;
import com.suncnpap.system.service.ConfigService;
import com.suncnpap.system.service.SchoolService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.app.util.beanutil.BeanUtil;
import com.app.util.common.Constants;
import com.app.util.common.EasyuiGrid;
import com.app.util.common.HandlerResult;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.reader.model.Reader;
import com.suncnpap.reader.model.ReaderType;
import com.suncnpap.reader.service.ReaderTypeService;

/** 
 * 说明：学校Controller
 */
//@Api(description = "学校操作接口")
@Controller
@RequestMapping(value="/system/school")
public class SchoolController extends BaseController {
	
	
	@Autowired
	SchoolService schoolService;
	
	@Autowired
	ReaderTypeService readerTypeService;
	
	@Autowired
	ConfigService configService;
	/**
	 * 去往列表页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
	
		HandlerResult result = new HandlerResult();//this.checkToken(request);
//		
//		if (result.getCode()==0) {

			Page page = this.setPage();
			page.removeQueryAnd("strSchoolId");
			
			String keyName = this.getParameter("keyName", "");
			String keywords = this.getParameter("keywords", "");
			if(!keywords.equals("")){
				page.addQueryAnd(keyName, QueryType.markLIKE, keywords);
			}
			
			page.setSort("dtCreatDate");
			page.setOrder("desc");
			
			page = schoolService.list(page);
			result = this.returnPage(result, page);
//		}
		
		ResponseUtil.writeJson(result, response);

	}



	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
//	@ApiOperation(value = "增加学校", notes="增加学校")
//    @ApiImplicitParam(name = "telephone", value = "电话号码", paramType = "query", required = true, dataType = "Integer")
	@RequestMapping(value="/add")
	public void add(@RequestBody School obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				init(obj);
				schoolService.add(obj);
				
				ReaderType readerType = new ReaderType();
				init(readerType);
				
				readerType.setStrName("学生");//类型名称  
				readerType.setIntNumOfBook(100);//图书可借数量  
				readerType.setIntNumOfJournal(100);//期刊可借数量  
				readerType.setdCardOpenFee(50.00);//开卡费用  
				readerType.setIntSpecialNumOfBook(100);//特殊图书可借数量，限制借阅数量
				readerType.setIntBorrowingDays(0);//借阅天数  
				readerType.setStrSchoolId(obj.getStrId());
				readerTypeService.add(readerType);
				
				init(readerType);
				readerType.setStrSchoolId(obj.getStrId());
				readerType.setStrName("教师");//类型名称  
				readerTypeService.add(readerType);
				
				Config config = new Config();
				init(config);
				config.setfBookOverdueFineAmount(30.00);
				config.setStrTitle(obj.getStrName());
				config.setIntEffectiveLength(6);  
				config.setIntEffectiveNumLength(6);
				config.setIntIsAppoint(0);
				config.setStrSchoolId(obj.getStrId());
				config.setIntMaxRenewCount(1);
				configService.add(config);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		
		ResponseUtil.writeJson(result, response);
	}
	
	
	/**
	 * 修改处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody School obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				schoolService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					schoolService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				School obj = schoolService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
}
